from pptx import Presentation
from pptx.dml.color import RGBColor
from pptx.enum.shapes import MSO_SHAPE_TYPE

def set_font_black(text_frame):
    """设置所有字体为黑色"""
    for paragraph in text_frame.paragraphs:
        for run in paragraph.runs:
            if run.font.color is not None:
                run.font.color.rgb = RGBColor(0, 0, 0)

def remove_images_from_shapes(shapes):
    """删除形状列表中的所有图片"""
    to_remove = []
    for shape in shapes:
        # 删除图片
        if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
            to_remove.append(shape)
        # 修改文本颜色
        elif shape.has_text_frame:
            set_font_black(shape.text_frame)
        # 表格内文本
        elif shape.shape_type == MSO_SHAPE_TYPE.TABLE:
            for row in shape.table.rows:
                for cell in row.cells:
                    set_font_black(cell.text_frame)
    # 删除图片形状
    for shape in to_remove:
        el = shape._element
        el.getparent().remove(el)

def clean_background(slide_or_layout_or_master):
    """清除背景填充（尽可能处理背景图片）"""
    try:
        fill = slide_or_layout_or_master.background.fill
        fill.solid()
        fill.fore_color.rgb = RGBColor(255, 255, 255)  # 白色背景
    except:
        pass

def process_ppt(input_path, output_path):
    prs = Presentation(input_path)

    # 清除母版和版式中的图片和背景
    for master in prs.slide_masters:
        clean_background(master)
        remove_images_from_shapes(master.shapes)
        for layout in master.slide_layouts:
            clean_background(layout)
            remove_images_from_shapes(layout.shapes)

    # 处理所有幻灯片
    for slide in prs.slides:
        clean_background(slide)
        remove_images_from_shapes(slide.shapes)

    prs.save(output_path)
    print(f"✅ 所有图片已删除，文字已变黑，保存为：{output_path}")

# 使用方式
input_ppt = 'C:\\Users\\gx_st\\Desktop\\汇川驱动器控制器\\01-第一章-第一节-企业与企业财务管理.pptx'        # 替换为你的文件路径
output_ppt = 'C:\\Users\\gx_st\\Desktop\\汇川驱动器控制器\\01-第一章-第一节-企业与企业财务管理1.pptx'    # 输出文件路径
process_ppt(input_ppt, output_ppt)
